﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmAdjust : Form
    {
        public FrmAdjust()
        {
            InitializeComponent();
        }

        private static Dictionary<string, WarehouseInfo> _dictWarehouse = null;
        public static Dictionary<string, WarehouseInfo>  dictWarehouse
        {
            get
            {
                if (_dictWarehouse == null) _dictWarehouse = new Dictionary<string, WarehouseInfo>();


                return _dictWarehouse;
            }
        }

        private static List<WarehouseInfo> _lstWarehouse = null;
        public static List<WarehouseInfo>  lstWarehouse
        {
            get
            {
                if (_lstWarehouse == null) _lstWarehouse = new List<WarehouseInfo> ();


                return _lstWarehouse;
            }
        }

        private void FrmAdjust_Load(object sender, EventArgs e)
        {
  
            
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            txtQty.Focus();
                
    
        }

        private void txtQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            string _Code = txtCode.Text.Trim();
            if (string.IsNullOrEmpty(_Code))
            {
                MessageBox.Show("商品编码不能为空！");
                return;
            }

            string _Qty = txtQty.Text.Trim();
            if (string.IsNullOrEmpty(_Qty))
            {
                MessageBox.Show("数量不能为空！");
                return;
            }

            decimal qty = Convert.ToDecimal(_Qty);
            if (qty <= 0)
            {
                MessageBox.Show("数量不能小于或等于0");
                return;
            }

            if (!dictWarehouse.Keys.Contains(_Code))
            {
                WarehouseInfo _warehouseInfo = new WarehouseInfo();
                _warehouseInfo.GOODSCODE = _Code;
                _warehouseInfo.QTY = qty;
                lstWarehouse.Add(_warehouseInfo);
                dictWarehouse.Add(_Code, _warehouseInfo);
            }
            else
            {
                WarehouseInfo _warehouseInfo = dictWarehouse[_Code];
                _warehouseInfo.GOODSCODE = _Code;
                _warehouseInfo.QTY = qty;
            }

            this.AdjustSource.DataSource = null;
            this.AdjustSource.DataSource = lstWarehouse;
            this.dbGridAdjust.DataSource = this.AdjustSource;
            this.dbGridAdjust.Refresh();

            txtCode.Text = string.Empty;
            txtQty.Text = string.Empty;
            txtCode.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstWarehouse != null && lstWarehouse.Count > 0 && MessageBoxEx.Question("确定提交数据！") == DialogResult.Yes)
                {
                    Cursor.Current = Cursors.WaitCursor;

                    int _result = Duty.DutyUIRule.NewInstance.UpdatePDAWarehouseQty(LoginBean.Instance.USERCODE, lstWarehouse);

                    if (_result > 0)
                    {
                        //清空数据
                        this.AdjustSource.DataSource = null;
                        this.dbGridAdjust.DataSource = null;
                        this.dbGridAdjust.Refresh();
                        lstWarehouse.Clear();       
                        dictWarehouse.Clear();

                        txtCode.Text = string.Empty;
                        txtQty.Text = string.Empty;
                        txtCode.Focus();

                    }
                    else
                    {
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("提交失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void FrmAdjust_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (MessageBoxEx.Question("确定退出吗？未提交的数据将丢失！") == DialogResult.Yes)
                {
                    System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
                    Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
                    Object o = Activator.CreateInstance(type);
                    System.Reflection.MethodInfo mi = type.GetMethod("Show");

                    mi.Invoke(o, null);

                    this.Close();
                    this.Dispose();
                }
            }
        }

        private void FrmAdjust_Closed(object sender, EventArgs e)
        {
            System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
            Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
            Object o = Activator.CreateInstance(type);
            System.Reflection.MethodInfo mi = type.GetMethod("Show");

            mi.Invoke(o, null);

            this.Close();
            this.Dispose();
        }


    }
}